﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Drifting.Graphics.EffectObjects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Drifting.Graphics.EffectObjects
{
    public class Grid : EffectObject
    {
        private Box gridLine;
        private Box lineX;
        private Box lineY;
        private Box lineZ;

        private bool xyz;
        private int gridN;
        private float gridDist;
        private List<Matrix> gridConfigs;
        
        public Grid(Matrix view, Matrix projection, GraphicsDevice device)
            : base(device)
        {
            hide = false;
            xyz = true;
        }

        override public void Draw(Matrix view, Matrix projection)
        {
            if (hide == true)
                return;

            for (int i = 0; i < gridConfigs.Count; ++i)
            {
                gridLine.SetConfig(gridConfigs[i]);
                gridLine.Draw(view, projection);
            }
            if (xyz == true)
            {
                lineX.Draw(view, projection);
                lineY.Draw(view, projection);
                lineZ.Draw(view, projection);
            }
        }

        public bool XYZ
        {
            get { return xyz; }
            set { xyz = value; }
        }

        public void setGrid(int n, float dist, double r_axle = 0.08)
        {
            gridDist = dist;
            gridN = n;
            gridLine = new Box(10, 0.005, 0.005, Color.Red, device);
            gridConfigs = new List<Matrix>();
            for (int i = 1; i <= gridN; ++i)
            {
                gridConfigs.Add(Matrix.CreateTranslation(0, 0, dist * i));
                gridConfigs.Add(Matrix.CreateTranslation(0, 0, dist * -i));

                gridConfigs.Add(Matrix.CreateTranslation(0, 0, dist * i) * Matrix.CreateRotationY(MathHelper.PiOver2));
                gridConfigs.Add(Matrix.CreateTranslation(0, 0, dist * -i) * Matrix.CreateRotationY(MathHelper.PiOver2));
            }
            gridConfigs.Add(Matrix.Identity);
            gridConfigs.Add(Matrix.CreateRotationY(MathHelper.PiOver2));

            lineX = new Box(5, r_axle, r_axle, Color.Red, device);
            lineX.SetConfig(Matrix.CreateTranslation(2.5f, 0, 0));
            lineY = new Box(r_axle, 5, r_axle, Color.Blue, device);
            lineY.SetConfig(Matrix.CreateTranslation(0, 0, 2.5f));
            lineZ = new Box(r_axle, r_axle, 5, Color.Green, device);
            lineZ.SetConfig(Matrix.CreateTranslation(0, 2.5f, 0));
        }

        public void setGrid(int n, float dist, float length, double r_axle = 0.08)
        {
            gridDist = dist;
            gridN = n;
            gridLine = new Box(length, 0.005, 0.005, Color.Red, device);
            gridConfigs = new List<Matrix>();
            for (int i = 1; i <= gridN; ++i)
            {
                gridConfigs.Add(Matrix.CreateTranslation(0, 0, dist * i));
                gridConfigs.Add(Matrix.CreateTranslation(0, 0, dist * -i));

                gridConfigs.Add(Matrix.CreateTranslation(0, 0, dist * i) * Matrix.CreateRotationY(MathHelper.PiOver2));
                gridConfigs.Add(Matrix.CreateTranslation(0, 0, dist * -i) * Matrix.CreateRotationY(MathHelper.PiOver2));
            }
            gridConfigs.Add(Matrix.Identity);
            gridConfigs.Add(Matrix.CreateRotationY(MathHelper.PiOver2));

            lineX = new Box(5, r_axle, r_axle, Color.Red, device);
            lineX.SetConfig(Matrix.CreateTranslation(2.5f, 0, 0));
            lineY = new Box(r_axle, 5, r_axle, Color.Blue, device);
            lineY.SetConfig(Matrix.CreateTranslation(0, 0, 2.5f));
            lineZ = new Box(r_axle, r_axle, 5, Color.Green, device);
            lineZ.SetConfig(Matrix.CreateTranslation(0, 2.5f, 0));
        }
    }
}
